#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>



typedef int MyCirQDataType;

typedef struct
{
    int front;
    int rear;
    MyCirQDataType* a;
    int k;
} MyCircularQueue;


bool myCircularQueueIsEmpty(MyCircularQueue* obj)
{
    assert(obj);
    return obj->rear == obj->front;
}

bool myCircularQueueIsFull(MyCircularQueue* obj)
{
    assert(obj);
    return (obj->rear + 1) % (obj->k + 1) == obj->front;
}
MyCircularQueue* myCircularQueueCreate(int k)
{
    MyCircularQueue* tmp1 = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if (tmp1 == NULL)
    {
        perror("myCircularQueueCreate::malloc::tmp1");
        exit(1);
    }
    MyCirQDataType* tmp = (MyCirQDataType*)malloc(sizeof(MyCirQDataType) * (k + 1));
    if (tmp == NULL)
    {
        perror("myCircularQueueCreate::malloc::tmp");
        exit(1);
    }
    tmp1->a = tmp;
    tmp1->front = 0;
    tmp1->rear = 0;
    tmp1->k = k;
    return tmp1;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value)
{
    assert(obj);
    if (myCircularQueueIsFull(obj))
    {
        return false;
    }

    obj->a[obj->rear] = value;
    obj->rear = (obj->rear + 1) % (obj->k + 1);

    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj)
{
    assert(obj);
    if (myCircularQueueIsEmpty(obj))
    {
        return false;
    }
    obj->front = (obj->front + 1) % (obj->k + 1);
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj)
{
    assert(obj);
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->a[obj->front];

}

int myCircularQueueRear(MyCircularQueue* obj)
{
    assert(obj);
    if (myCircularQueueIsEmpty(obj))
    {
        return -1;
    }
    return obj->a[(obj->rear - 1 + obj->k + 1) % (obj->k + 1)];
}



void myCircularQueueFree(MyCircularQueue* obj)
{
    free(obj->a);
    obj->a = NULL;
    free(obj);
    obj = NULL;
}
/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);

 * bool param_2 = myCircularQueueDeQueue(obj);

 * int param_3 = myCircularQueueFront(obj);

 * int param_4 = myCircularQueueRear(obj);

 * bool param_5 = myCircularQueueIsEmpty(obj);

 * bool param_6 = myCircularQueueIsFull(obj);

 * myCircularQueueFree(obj);
*/
int main()
{
    MyCircularQueue* obj = myCircularQueueCreate(3);
    bool param_1 = myCircularQueueEnQueue(obj, 2);
    int param_5 = myCircularQueueRear(obj);

    int param_3 = myCircularQueueFront(obj);

   /* bool param_2 = myCircularQueueEnQueue(obj, 2);
    bool param_3 = myCircularQueueEnQueue(obj, 3);
    bool param_4 = myCircularQueueEnQueue(obj, 4);*/
    //bool param_6 = myCircularQueueIsFull(obj);
    //bool param_7 = myCircularQueueDeQueue(obj);
    //bool param_8 = myCircularQueueEnQueue(obj, 4);
    //int param_9 = myCircularQueueRear(obj);

    //bool param_5 = myCircularQueueIsEmpty(obj);
    myCircularQueueFree(obj);
    return 0;
}